package cn.panda.edu.teacher;

import cn.panda.edu.model.Select;
import cn.panda.edu.service.SelectService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 
 * 类描述：  对选择题的CURD操作
 */
@RestController
@RequestMapping(value = "/api/teacher/select")
public class TeacherSelectController {
	
	@Autowired
	private SelectService selectService;

	/**
	 * @return List<Notice>
	 * @Title: getList
	 * @Description: 查询所有公告
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Object getList(Integer pageIndex, Integer courseId) {
		if (pageIndex == null) {
			pageIndex = 1;
		}
		PageInfo<Select> pageInfo = selectService.listSelectByCourseId(courseId, pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200, "", pageInfo);
	}


	@RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
	public Object selectByPrimaryKey(Integer selectId) {
		Select select = selectService.selectByPrimaryKey(selectId);
		return ResultDataUtil.resData(200, "", select);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Object updateByPrimaryKeySelective(@RequestBody Select record) {
		int status=200;
		String message=null;
		try {
			selectService.updateByPrimaryKeySelective(record);
			status=200;
			message="更新成功!";
		} catch (Exception e) {
			status=500;
			message="更新失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public  Object deleteByPrimaryKey(Integer selectId) {
		int status=200;
		String message=null;
		try {
			selectService.deleteByPrimaryKey(selectId);
			status=200;
			message="删除成功!";
		} catch (Exception e) {
			status=500;
			message= "删除失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Object insertSelective(@RequestBody Select record) {
		int status=200;
		String message=null;
		try {
			record.setSelectAddtime(new Date());
			record.setSelectDelete(1);
			selectService.insertSelective(record);
			status=200;
			message="添加成功!";
		} catch (Exception e) {
			status= 500;
			message="添加失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}
}
